import Vue from 'vue'
import Router from 'vue-router'

import { Loading, Error } from '@/components/Common'

// const lazyLoading = (name, index = false) => (resolve) => require([`views/${name}${index ? '/index' : ''}.vue`], resolve)
const lazyLoading = (name, index = false) => () => ({
  // 需要加载的组件. 应当是一个 Promise
  component: import(`@/views/${name}${index ? '/index' : ''}.vue`),
  // loading 时应当渲染的组件
  loading: Loading,
  // 出错时渲染的组件
  error: Error,
  // 渲染 loading 组件前的等待时间。默认：200ms.
  delay: 200,
  // 最长等待时间。超出此时间则渲染 error 组件。默认：Infinity
  timeout: 3000
})

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Home',
      component: lazyLoading('Home', true)
    }, {
      path: '/live',
      name: 'Live',
      component: lazyLoading('Live', true)
    }, {
      path: '/lock',
      name: 'Lock',
      component: lazyLoading('App/lock')
    }, {
      path: '/assistant',
      name: 'Assistant',
      component: lazyLoading('Assistant', true)
    }, {
      path: '/login',
      name: 'Login',
      meta: {
        auth: true
      },
      component: lazyLoading('Login', true)
    }, {
      path: '/analysis',
      name: 'Analysis',
      component: lazyLoading('Analysis', true)
    }, {
      path: '/app',
      name: 'App',
      component: lazyLoading('App', true)
    }, {
      path: '/category',
      name: 'Category',
      component: lazyLoading('Category', true)
    }, {
      path: '/recommend',
      name: 'Recommend',
      component: lazyLoading('Recommend', true)
    }, {
      path: '/push',
      name: 'Push',
      component: lazyLoading('Push', true)
    }, {
      path: '/futurePush',
      name: 'FuturePush',
      component: lazyLoading('Push/futures')
    },
    { path: '*', redirect: '/' }
  ]
})
